#!/bin/sh

#
# You do *NOT* need to modify this file.
#

# Notes:
#   * Use function 'extract_pkg' to extract a tarball instead of use
#     tar/bzip2/gzip directly.
#
#   * Use function 'install_pkg' instead of use 'pkg_add' directly.
#

# ----------------------------------------------------------
# some small useful functions.
# ----------------------------------------------------------
ECHO_INFO()
{
	echo    "============"
    echo -e "*** INFO *** $@"
	echo    "============"
}

ECHO_ERROR()
{
	echo    "!!!!!!!!!!!!!"
    echo -e "!!! ERROR !!! $@"
	echo    "!!!!!!!!!!!!!"
}

install_pkg()
{
    if [ X"$#" = X"0" ]; then
        echo "Usage:"
        echo -e "\tinstall_pkg pkg_name /path/to/package/port/"
        echo -e "\tinstall_pkg pkg_name /path/to/package/port 'COMMAND_TO_INSTALL_PORT(with quote)'"
    elif [ $# -ge 1 ]; then
        echo "===================="
        echo "*** INSTALLATION *** Install package: $1."
        echo "===================="
        pkg_add $1

        if [ X"$?" != X"0" ]; then
            ECHO_ERROR "Installation failed."
            ECHO_INFO "PKG_PATH: ${PKG_PATH}."

            if [ X"$#" = X"1" ]; then
                ECHO_ERROR "Not more installation method available. Abort."
            elif [ X"$#" = X"2" ]; then
                # Install package from port.
        		echo "===================="
                echo "*** INSTALLATION *** Install package($1) from port: $2."
        		echo "===================="
                cd $2 && make install clean
            elif [ $# -ge 3 ]; then
        		echo "===================="
                echo "*** INSTALLATION *** Install package: $1."
        		echo "===================="
				ECHO_INFO "Port: $2"
				ECHO_INFO "Build command: $@"
                cd $2 && shift 2 && eval "$@"
            fi
        fi
    fi
}

fetch_tarball()
{
    # Usage:
    #   fetch_tarball FILENAME
    #   fetch_tarball FILENAME URL_FOR_DOWNLOAD
    #   fetch_tarball file1.tar.gz http://xxx.file1.tar.gz
    #
    # How it works:
    #   *) CMD: fetch_tarball $1 $2
    #   1) ${FETCH_CMD} ${MISC_PATH}/$1
    # if step1 failed, then
    #   2) ${FETCH_CMD} $2
    
    cd ${PKG_DIR}

    if [ X"$#" = X"2" ]; then
        ECHO_INFO "Fetch file from: ${MISC_PATH}/$1"
        ${FETCH_CMD} ${MISC_PATH}/$1

        if [ X"$?" != X"0" ]; then
            ECHO_ERROR "Fetch failed. Try: $2"
            ${FETCH_CMD} $2
        fi
    elif [ X"$#" = X"1" ]; then
        ECHO_INFO "Fetch file from: ${MISC_PATH}/$1"
        ${FETCH_CMD} ${MISC_PATH}/$1
    else
        ECHO_ERROR "Skip. No URL given."
    fi
}

extract_pkg()
{
    if [ X"$2" = X"" ]; then
        DST='.'
    else
        DST="$2"
    fi

    if echo $1 | grep '.tar.gz$' 2>/dev/null ; then
        ECHO_INFO "Extract package $1 to ${DST}"
        tar zxf $1 -C $DST
    elif echo $1 | grep '.tar.bz2$' 2>/dev/null ; then

        # Install bzip2 first.
        [ -x /usr/local/bin/bzip2 ] || install_pkg bzip2 ${PORT_BZIP2}

        ECHO_INFO "Extract package $1 to ${DST}"
        bzip2 -d $1
        tar xf $(echo $1 | sed 's/\.bz2$//') -C $DST
    else
        ECHO_ERROR "Unknown archive format."
    fi
}

prompt_and_config()
{
    ECHO_INFO "You may need ports tree to build these packages:"

    for i in ${PORTS_LIST}; do
        if [ -d $i ]; then
            printf "\t* %-60s%s\n" $i "[ OK ]"
        else
            printf "\t* %-60s%s\n" $i "[ NOT EXIST ]"
        fi
    done

    ECHO_INFO "If you have packages build and store in PKG_PATH, you don't need ports tree."
    echo -n "Continue? [Y|n]"
    read ANSWER

    case $ANSWER in
        N|n ) echo "Exit."; exit 255 ;;
        Y|y|* )
            ECHO_INFO "Use 'dialog' for setting."
            chmod +x ${CONFIG_VIA_DIALOG} && \
            install_pkg dialog ${PORT_DIALOG} && \
            . ${CONFIG_VIA_DIALOG}
            ;;
    esac
}

# ------------------------------------------------
# ---- Check necessery files/dirs.
# ------------------------------------------------
check_env()
{
    echo -n "Check configuration file: ${CONFIG_FILE}..."
    if [ -f ${CONFIG_FILE} ]; then
        echo -e "\tFound."
        echo -n "Use it for mail server setting? [Y|n]"
        read ANSWER

        case  $ANSWER in
            N|n )
                echo "Skip configuration file: ${CONFIG_FILE}."
                prompt_and_config
                ;;
            Y|y|* )
                echo "Use configuration file: ${CONFIG_FILE} for mail server setting."
                . ${CONFIG_FILE}
                ;;
        esac
    else
        echo -e "\tNone."
        prompt_and_config
    fi
}
